import axios from 'axios';
import { post, get } from '@/utils/request';

interface FileRresponse {
  code: number;
  msg: string;
  status: boolean;
  data: any;
  fail: boolean;
}

interface DocumentInfo {
  id: number;
  fileName: string;
  filePath: string;
  directoryId: number;
  userId: number;
  uploadDateTime?: string;
  pageNo: number;
  pageSize: number;
  html: string;
}

/**
 * 上传附件
 * @returns 公共返回结果
 */
export const addDocumentInfo = async (file: any, directoryId: number): Promise<FileRresponse> => {
	const token = localStorage.getItem('token');
  const formData = new FormData();
  formData.append('file', file);
  formData.append('fileName', file.name);
  formData.append('uid', file.uid);
  formData.append('directoryId', directoryId.toString());
  return axios.post(`/api/starhub/document/addDocumentInfo`, formData,{
		headers: {
			'Authorization':`Bearer ${token}`
		},
	});
};

export const converHtml2Word = async (data: Partial<DocumentInfo>): Promise<FileRresponse> => {
  return post(`/api/starhub/document/convertHtmlToDocxAdvanced`, data);
};

export const getContentByFileId = async (data: DocumentInfo): Promise<FileRresponse> => {
  return post(`/engine/starhub/document/getContentByFileId`,data);
}

export const getHtmlByFileId = async (data: DocumentInfo): Promise<FileRresponse> => {
  return post(`/engine/starhub/document/getHtmlByFileId`,data);
}

export const generateDownloadToken = async (fileId: any): Promise<FileRresponse> => {
  return get(`/api/starhub/document/generateDownloadToken/${fileId}`, {});
}
